﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类PrvIPAccessList。
    /// </summary>
    public class PrvIPAccessList
    {
        public PrvIPAccessList()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("FAccessIPID", "t_PrvIPAccessList");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FEntityNo, int FAccessIPID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_PrvIPAccessList");
            strSql.Append(" where FEntityNo=@FEntityNo and FAccessIPID=@FAccessIPID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FAccessIPID", SqlDbType.Int,4)};
            parameters[0].Value = FEntityNo;
            parameters[1].Value = FAccessIPID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.PrvIPAccessList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_PrvIPAccessList(");
            strSql.Append("FEntityNo,FStartIP,FEndIP,FNote)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FStartIP,@FEndIP,@FNote)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FStartIP", SqlDbType.BigInt,8),
					new SqlParameter("@FEndIP", SqlDbType.BigInt,8),
					new SqlParameter("@FNote", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FStartIP;
            parameters[2].Value = model.FEndIP;
            parameters[3].Value = model.FNote;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.PrvIPAccessList model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_PrvIPAccessList set ");
            strSql.Append("FStartIP=@FStartIP,");
            strSql.Append("FEndIP=@FEndIP,");
            strSql.Append("FNote=@FNote");
            strSql.Append(" where FEntityNo=@FEntityNo and FAccessIPID=@FAccessIPID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FAccessIPID", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FStartIP", SqlDbType.BigInt,8),
					new SqlParameter("@FEndIP", SqlDbType.BigInt,8),
					new SqlParameter("@FNote", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.FAccessIPID;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FStartIP;
            parameters[3].Value = model.FEndIP;
            parameters[4].Value = model.FNote;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FEntityNo, int FAccessIPID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_PrvIPAccessList ");
            strSql.Append(" where FEntityNo=@FEntityNo and FAccessIPID=@FAccessIPID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FAccessIPID", SqlDbType.Int,4)};
            parameters[0].Value = FEntityNo;
            parameters[1].Value = FAccessIPID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.PrvIPAccessList GetModel(string FEntityNo, int FAccessIPID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FAccessIPID,FEntityNo,FStartIP,FEndIP,FNote from t_PrvIPAccessList ");
            strSql.Append(" where FEntityNo=@FEntityNo and FAccessIPID=@FAccessIPID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FAccessIPID", SqlDbType.Int,4)};
            parameters[0].Value = FEntityNo;
            parameters[1].Value = FAccessIPID;

            Model.PrvIPAccessList model = new Model.PrvIPAccessList();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FAccessIPID"].ToString() != "")
                {
                    model.FAccessIPID = int.Parse(ds.Tables[0].Rows[0]["FAccessIPID"].ToString());
                }
                model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                if (ds.Tables[0].Rows[0]["FStartIP"].ToString() != "")
                {
                    model.FStartIP = long.Parse(ds.Tables[0].Rows[0]["FStartIP"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FEndIP"].ToString() != "")
                {
                    model.FEndIP = long.Parse(ds.Tables[0].Rows[0]["FEndIP"].ToString());
                }
                model.FNote = ds.Tables[0].Rows[0]["FNote"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAccessIPID,FEntityNo,FStartIP,FEndIP,FNote ");
            strSql.Append(" FROM t_PrvIPAccessList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FAccessIPID,FEntityNo,FStartIP,FEndIP,FNote ");
            strSql.Append(" FROM t_PrvIPAccessList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_PrvIPAccessList";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

